package com.wupaas.boot.core.business.intercept;

import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.wupaas.boot.core.business.context.EnvironmentContextHolder;
import com.wupaas.boot.core.business.security.jwt.JwtUtil;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.StringValue;

/**
 * @author JueYue
 * @date 2022-05-22-5-19
 * @since 1.0
 */
public class TenantLineHandlerImpl implements TenantLineHandler {

    private static String TENANT_LINE = "mybatis-plus.open.tenant-line";

    @Override
    public Expression getTenantId() {
        return new StringValue(JwtUtil.getTenantId());
    }

    /**
     * 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件
     *
     * @param tableName
     * @return
     */
    @Override
    public boolean ignoreTable(String tableName) {
        return !EnvironmentContextHolder.getBool(TENANT_LINE)
                || tableName.startsWith("t_system")
                || tableName.startsWith("t_code")
                || tableName.startsWith("t_base");
    }
}
